<?php

namespace app\mobile\controller;

class ResearchCopy extends Base
{
    //前置方法
    protected $beforeActionList = [
        'rquestion' => ['only' => 'detailed'],
        'right' => ['only' => 'index,detailed'],

    ];

    public function rquestion()
    {
        $id = $this->request->param('id');


        $list = cache('question_copy'.$id . cookie('think_var'));

        if(empty($list1)) {
            $rqdata = db('Research_question_copy')->where(array('rid' => $id))->order('id asc')->select();
            $useqids = $list = array();
            $time=substr(time(),-1);
            foreach ($rqdata as $v) {
                $useqids[] = $v['id'];
                switch ($time){
                    case '1':
                        $oredr = 'name asc';
                        break;
                    case '3':
                    case '5':
                        $oredr = 'id desc';
                        break;
                    case '7':
                    case '9':
                        $oredr = 'id asc';
                        break;
                    case '2':
                    case '4':
                        $oredr = 'question asc';
                        break;
                    case '6':
                    case '8':
                        $oredr = 'question desc';
                        break;
                    default:
                        $oredr = 'name desc';
                        break;
                }
                $answerobj = db('Research_answer_copy')->where(array('qid' => $v['id']))->order($oredr)->select();
                $v['answer'] = $answerobj;
                $v['rowspan'] = count($v['answer']);
                $list[] = $v;
            }
            cache('question_copy'.$id . cookie('think_var'),$list,600);

        }

        $this->assign([
                'list'=>$list,
            ]
        );

    }


    /**
     * 保存调研结果
     */
    public function saveresearch()
    {

        //登陆考试
        $Admin = session('User', '', 'User');
        if(empty($Admin->id)){
            return ajax_info('请登录考试','y',url('user/login/index'));
        }
        $oo = $this->request->param();

//        exit();
//        $dateline = db('research_copy')->where('id', $oo['id'])->value('dateline');
//        if($dateline<time()){
//            return ajax_info('已经超过时间','u');
//        }
//
//        $o = db('research_result')->where('rid', $oo['id'])->find();
//        if ($o) {
//            return ajax_info('已经提交过', 'u');
//        }
//
//
        if (empty($oo['oid'])) {
            return ajax_info('最少做一个选择');
        }



        foreach ($oo['oid'] as $k => $v) {
            if (is_array($v)) {
//                print_r($v);
                $vvdate =array();
                foreach ($v as  $vv) {
                    $vvdate[] = $vv;
                }
                $value[$k] = implode(',',$vvdate);

            } else {
                $value[$k] = $v;
            }
        }
//        $date = implode(',', $value);

//        print_r($data);
//        print_r($oo);
//        print_r($date);
//        print_r(json_encode($value));
//
//        print_r($value);
//
//        exit();
        $data = array(
            'rid' => $oo['id'],
            'qid' => $Admin->id,
            'username' => $Admin->name,
            'title' => $oo['title'],
            'content' => json_encode($value),
            'lang' => cookie('think_var'),
            'create_time' => time(),
        );
        db('research_result_copy')->insert($data);

        return ajax_info('成功交卷', 'u');


    }



}
